#!/bin/sh

set -e
set -u

# Get LANG
. /etc/default/locale
export LANG

# Initialize gettext support
. gettext.sh
TEXTDOMAIN="tails"
export TEXTDOMAIN

# Import wait_until()
. /usr/local/lib/tails-shell-library/common.sh
# Import i2p_built_a_tunnel, i2p_reseed_failed, i2p_router_console_is_ready(),
# and set_best_i2p_router_console_lang().
. /usr/local/lib/tails-shell-library/i2p.sh

I2P_STARTUP_TIMEOUT=60

# When there are network problems (either local or remote), it can take up to 3
# minutes for all of the current reseed servers to be tried.
I2P_BOOTSTRAP_TIMEOUT=210

# After the router infos (RIs) are downloaded from the reseed servers
# it can take 3-5 minutes for a tunnel to be built, e.g.
# once we get to this point I2P should be ready to be used.
I2P_TUNNEL_BUILD_TIMEOUT=300

startup_failure() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P failed to start\"`" \
        "`gettext \"Something went wrong when I2P was starting. Check the logs in /var/log/i2p for more information.\"`"
    service i2p dump # generate a thread dump
    sleep 5 # Give thread dump ample time to complete
    systemctl stop i2p # clean up, just in case
    exit 1
}

wait_until_i2p_router_console_is_ready() {
    wait_until ${I2P_STARTUP_TIMEOUT} i2p_router_console_is_ready
}

wait_until_i2p_has_bootstrapped() {
    wait_until ${I2P_BOOTSTRAP_TIMEOUT} '[ "$(i2p_reseed_status)" = success ]'
}

notify_router_console_success() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P's router console is ready\"`" \
        "`gettext \"You can now access I2P's router console in the I2P Browser.\"`"
}

bootstrap_failure() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P is not ready\"`" \
        "`gettext \"Eepsite tunnel not built within six minutes. Check the router console in the I2P Browser or the logs in /var/log/i2p for more information. Reconnect to the network to try again.\"`"
    exit 1
}

wait_until_i2p_builds_a_tunnel() {
    wait_until ${I2P_TUNNEL_BUILD_TIMEOUT} i2p_built_a_tunnel
    # static sleep to work around upstream bug.
    sleep 240
}

notify_bootstrap_success() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P is ready\"`" \
        "`gettext \"You can now access services on I2P.\"`"
}

case "${1}" in
    start|restart)
        # Stop I2P before setting the router console language in case
        # it pushes any updated options on quit.
        if systemctl --quiet is-active i2p; then
            systemctl stop i2p
        fi
        # Get LANG, since we may run this from an environment that
        # doesn't have it set.
        . /etc/default/locale
        set_best_i2p_router_console_lang
        systemctl start i2p
        wait_until_i2p_router_console_is_ready || startup_failure
        notify_router_console_success
        wait_until_i2p_has_bootstrapped || bootstrap_failure
        wait_until_i2p_builds_a_tunnel || bootstrap_failure
        notify_bootstrap_success
        ;;
    stop)
        exec systemctl stop i2p
        ;;
    *)
        echo "invalid argument '${1}'" >&2
        exit 1
        ;;
esac
